/*
 * @Author: zhang zhijiang 2824735304@qq.com
 * @Date: 2025-05-20 19:37:46
 * @LastEditors: zhang zhijiang 2824735304@qq.com
 * @LastEditTime: 2025-05-20 20:15:01
 * @FilePath: /linux/minihttp/httpserver.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "sock.hpp"
#define PORT 8081
#include<pthread.h>
#include"protocol.hpp"

class httpserver
{
public:
    httpserver(int port = PORT) : _port(port), _sock(nullptr), stop(false)
    {
    }
    void init()
    {
        _sock = sock::getinstance(_port);
    }
    void loop()
    {
        int listen_sock = sock::sockfd();
        while (!stop)
        {
            sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sockfd = accept(listen_sock, (sockaddr *)&peer, &len);
            if (sockfd < 0)
            {
                continue;
            }
            int *_sockfd = new int(sockfd);//避免值拷贝被修改
            pthread_t tid;
            pthread_create(&tid,nullptr,entrance::handler_request,_sockfd);
            pthread_detach(tid);
        }
    }
    ~httpserver()
    {
    }

private:
    sock *_sock;
    int _port;
    bool stop;
};